package com.nifostasky.pattern.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 扇形交换机配置
 *  发布订阅模式
 */
@Configuration
public class RabbitFanoutConfig {
    //交换机名称
    public static final String EXCHANGE_NAME = "FANOUT_EXCHANGE";
    //队列名称
    public static final String QUEUE_NAME = "FANOUT_QUEUE";
    public static final String QUEUE_NAME_1 = "FANOUT_QUEUE_1";

    /**
     * 1.交换机
     *  添加一台扇形交换机
     */
    @Bean(EXCHANGE_NAME)
    public Exchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(EXCHANGE_NAME).durable(true).build();
    }


    /**
     * 2.Queue 队列
     *  添加第一个队列
     */
    @Bean(QUEUE_NAME)
    public Queue fanoutQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    /**
     * 2.1 Queue 队列
     *  添加第两个队列
     */
    @Bean(QUEUE_NAME_1)
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(QUEUE_NAME_1).build();
    }

    /**
     * 3. 队列和交互机绑定关系 Binding
     *  将第一个队列绑定到 扇形交换机上
     */
    @Bean
    public Binding bindFanoutExchange(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        // fanout :routing key 默认为 "",指定了别的值也没用
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    /**
     * 3.1 队列和交互机绑定关系 Binding
     *  将第二个队列绑定到 扇形交换机上
     */
    @Bean
    public Binding bindFanoutExchange1(@Qualifier(QUEUE_NAME_1) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        // fanout :routing key 默认为 "",指定了别的值也没用，我们这里随便写个值，看会不会有影响
        return BindingBuilder.bind(queue).to(exchange).with("aaabbb").noargs();
    }
}
